package com.mdoya.controller;

import com.mdoya.domain.User;
import com.mdoya.exception.BusinessExceptiion;
import com.mdoya.response.ResultResponse;
import com.mdoya.service.UserService;
import com.mdoya.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Time;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    /**
     *   获取手机登录验证码
     * @param phone
     * @param session
     * @return
     */
    @GetMapping("/sendCode")
    public ResultResponse sendCode(String phone, HttpSession session) {
        log.info("手机号码获取验证码: ", phone);
        String code = ValidateCodeUtils.generateValidateCode4String(4);
        if (StringUtils.isNotEmpty(code)) {
//            session.setAttribute(phone, code);
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        }
        System.err.println("您的验证码: " + code + " 三分钟内有效");
        return ResultResponse.success(code);
    }

    /**
     *  前端用户登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public ResultResponse userLogin(@RequestBody Map<String, String> map, HttpSession session) {
        log.info("用户登录: {}", map);
        if (checkMapParams(map,session)){
            User user = userService.login(map);
            session.setAttribute("user",user);
            return ResultResponse.success(user);
        }
        return ResultResponse.error("登录失败");
    }
    /**
     *  前端用户退出登录
     * @param session
     * @return
     */
    @GetMapping("/loginout")
    public ResultResponse userLogout(HttpSession session) {
        log.info("用户退出登录: {}");
        System.err.println("用户退出登录");
        User user = (User) session.getAttribute("user");
        if (user != null){
            session.removeAttribute("user");
            return ResultResponse.success("成功退出登录");
        }
        return ResultResponse.error("退出失败");
    }

    /**
     *  校验登录参数
     * @param map 集合
     * @param session 一次会话，可以持久化
     * @return
     */
    public boolean checkMapParams(Map<String, String> map, HttpSession session) {
        String phone = map.get("phone");
        String code = map.get("code");
        if (!StringUtils.isNotEmpty(phone)) throw new BusinessExceptiion(-1,"手机号码不能为空！");
        if (!StringUtils.isNotEmpty(code)) throw new BusinessExceptiion(-1,"验证码不能为空！");
//        String oldCode = (String) session.getAttribute(phone);
        String oldCode = (String)redisTemplate.opsForValue().get(phone);
        System.err.println("redis中oldCode = " + oldCode);
        if (!oldCode.equalsIgnoreCase(oldCode)) throw new BusinessExceptiion(-1,"输入验证码不正确！");
        return true;
    }
}
